package com.webkit.web.action;

import com.webkit.web.ActionContext;
import java.util.Map;

public abstract class ServletAction extends Action
{
  public abstract ServletActionResult execute();

  protected ServletActionResult error(String url)
  {
    return dispatch(url);
  }

  protected ServletActionResult error(String url, boolean redirect) {
    if (redirect) {
      return redirect(url);
    }
    return dispatch(url);
  }

  protected ServletActionResult error(String url, String message) {
    if (message != null) {
      setErrorMessage(message);
    }
    return dispatch(url);
  }

  protected ServletActionResult success(String url) {
    return dispatch(url);
  }

  protected void setMessage(String message) {
    ActionContext.getActionContext().getRequest().put("message", message);
  }

  protected void setErrorMessage(String message) {
    ActionContext.getActionContext().getRequest().put("errorMessage", message);
  }

  protected ServletActionResult success(String url, String message) {
    if (message != null) {
      setMessage(message);
    }
    return dispatch(url);
  }

  protected ServletActionResult success(String url, boolean redirect) {
    if (redirect) {
      return redirect(url);
    }
    return dispatch(url);
  }

  protected ServletActionResult redirect(String url) {
    return new RedirectActionResult(url);
  }

  protected ServletActionResult dispatch(String url) {
    return new DispatcherActionResult(url);
  }
}